package com.woniuxy.main.handler;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.session.Session;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.main.pojo.Order;
import com.woniuxy.main.pojo.Page;
import com.woniuxy.main.pojo.Vi_or_co_le;
import com.woniuxy.main.pojo.Vi_or_stu_le;
import com.woniuxy.main.service.OrderService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Controller
@RequestMapping("/order")
@Scope("prototype")
@Data
public class OrderHandler {

	@Resource
	private OrderService orderService;
	
	//学员确认已上完课
	@RequestMapping("/confirm")
	@ResponseBody
	public String confirm(Order order){
		String result = "确认失败！";
		int row = orderService.confirm(order);
		if(row > 0){
			result = "确认成功！";
		}
		return result;
	}
	//学员确认付款
	@RequestMapping("/payment")
	@ResponseBody
	public String payment(Order order,String password){
		return orderService.payment(order,password);
	}

	//分页显示订单
	@RequestMapping("/studentOrder")
	@ResponseBody
	public Page<Vi_or_co_le> studentOrder(Page page,int status,int uid){
		return orderService.studentOrder(page,status,uid);
	}
	
	//分页显示订单---教练
	@RequestMapping("/coachOrder")
	@ResponseBody
	public Page<Vi_or_stu_le> coachOrder(Page page,int status,int uid){
		return orderService.coachOrder(page,status,uid);
	}
	
	//更改订单(取消订单申请、退款申请、取消退款中的订单、撤回取消中的订单)
	@RequestMapping("/updateOrder")
	@ResponseBody
	public String updateOrder(Order order){
		String result = "发送请求失败！";
		int row = orderService.updateOrder(order);
		if(row > 0){
			result = "发送请求成功，等待教练确认！";
		}
		return result;
	}
	
	//删除订单
	@RequestMapping("/deleteOrder")
	@ResponseBody
	public String deleteOrder(Order order){
		String result = "删除订单失败！";
		int row = orderService.deleteOrder(order);
		if(row > 0){
			result = "删除订单成功！";
		}
		return result;
	}
	
	//教练确认已上完课
	@RequestMapping("/confirmFinish")
	@ResponseBody
	public String confirmFinish(Order order){
		String result = "确认失败！";
		int row = orderService.updateOrder(order);
		if(row > 0){
			result = "确认成功！";
		}
		return result;
	}
	
	//教练同意取消订单
	@RequestMapping("/agreeCancle")
	@ResponseBody
	public String agreeCancle(Order order){
		String result = "取消失败！";
		order.setUpdateDate(new SimpleDateFormat("y-M-d HH:mm:ss").format(new Date()));
		int row = orderService.updateOrder(order);
		if(row > 0){
			result = "取消成功！";
		}
		return result;
	}
	
	//教练同意退款
	@RequestMapping("/agreeRefund")
	@ResponseBody
	public String agreeRefund(Order order){
		String result = "退款失败！";
		int row = orderService.updateOrder(order);
		if(row > 0){
			result = "退款成功！";
		}
		return result;
	}
	
	//教练确认付款
	@RequestMapping("/payment2")
	@ResponseBody
	public String payment2(Order order,String password){
		return orderService.payment2(order,password);
	}

	//教练拒绝
	@RequestMapping("/refuse")
	@ResponseBody
	public String refuse(Order order){
		String result = "拒绝失败！";
		order.setUpdateDate(new SimpleDateFormat("y-M-d HH:mm:ss").format(new Date()));
		int row = orderService.updateOrder(order);
		if(row > 0){
			result = "拒绝成功！";
		}
		return result;
	}	
			
	//生成订单
	@RequestMapping("/insertOrder")
	@ResponseBody
	public String insertOrder(Order order){
		String result = "下单失败！";
		//生成下单时间
		String createDate = new SimpleDateFormat("y-M-d HH:mm:ss").format(new Date());
		order.setCreateDate(createDate);
		int row = orderService.insertOrder(order);
		if(row > 0){
			result = "下单成功！";
		}
		return result;
	}
}
